package org.yagnus.scalasupport.maps

import java.util.Map;

/**
 * A WrappedJavaMap is exactly a Map from java, except we've added a 
 * method called `lookup` which returns an optional result matching 
 * scala map's {@link scala.collection.map#get} method. See example in {@link org.yagnus.scala.derivatives.map.WrappedJavaLinkedHashMap 
 * for usage example. 
 *  
 *  NOTE: The default implementation contained here in are not threadsafe.
 * 
 * 
 * @author hc.busy
 *
 */
trait WrappedJavaMap[K, V] extends Map[K, V] {
    protected type Entry = Map.Entry[K, V];

    //This is like the get of a scala hash
    def lookup(key : K) : Option[V] = {
        if (!containsKey(key)) return None;
        else return Some(get(key));
    }

    /**
     * got is a shorthand for lookup, same letter count as get
     * @param key
     * @return
     */
    def got(key : K) : Option[V] = lookup(key);

    /**
     * if Map contains key, this method returns the value. Otherwise, it evaluates the second parameter, inserts
     * the result of the evaluation into the map as the value associated with the key in the parameter, and returns
     * the value as the result.
     * 
     * @param k key to check in the map
     * @param v value to use if key was not found, also sets this value to be associated with the key
     */
    def getOrSet(k : K, v : ⇒ V) : V = {
        if (!containsKey(k)) return get(k);
        else {

            val evaluated : V = v;
            put(k, evaluated);

            return evaluated;
        }
    }

    /**
     * gos is a shorthand for getOrSet
     * 
     * @param k
     * @param v
     * @return
     */
    def gos(k : K, v : ⇒ V) : V = getOrSet(k, v);

}